<?php

/**
 * Class Order
 * 渝北公安局，需要的订单详情
 */
class Order
{

    public static function index($file)
    {

        $PHPExcel = PHPExcel_IOFactory::load($file);
        // 读取第一個工作表
        $sheet = $PHPExcel->getSheet(0);
        // 取得总行数
        $highestRow = $sheet->getHighestRow();
        // 最高列英文字母
        $highestColumm = $sheet->getHighestColumn();

        $filed = [];
        $fieldValues = [];
        for ($col = 0; $col < $highestRow; $col++) {
            // 从第三行开始取值
            $keys = $sheet->getCellByColumnAndRow($col, 3)->getValue();
            if (!empty($keys)) {
                $filed[$col] = $keys;
            }
        }

        $list = count($filed);
        $data = [];
        for ($row = 4; $row < $highestRow; $row++) {
            for ($col = 0; $col < $list; $col++) {
                $val = $sheet->getCellByColumnAndRow($col, $row)->getValue();
                $fieldValues[$col] = (string)$val;
            }
            $data[] = array_combine($filed, $fieldValues);
        }


        $json = [
            "USERNAME" => "wbdw_txz",
            "PASSWORD" => "tTxz$$19",
            "SFCS" => "1",
            "DATA_TYPE" => "1880",
            "DATA_STR" => ""
        ];

        $json['DATA_STR'] = json_encode($data, JSON_UNESCAPED_UNICODE);

        return json_encode($json, JSON_UNESCAPED_UNICODE);

    }
}

// 定义加载文件
define('APP_ROOT', __DIR__);
include APP_ROOT . '/../vendor/autoload.php';
// 加载文件
$file = APP_ROOT . '/basedata/data.xlsx';


header("content-type:text/html;charset=utf-8");
$time_ago = microtime(true);
try {
    if (!file_exists($file)) {
        exit("not found {$file}.\n");
    }
    $file_path = realpath($file);

    $result_data = Order::index($file_path);

//    echo $result_data;die;

    //解决OpenSSL Error问题需要加第二个array参数，具体参考 http://stackoverflow.com/questions/25142227/unable-to-connect-to-wsdl
    $client = new SoapClient("http://222.177.119.188:9888/batchNet/webservice/WbHlwWebservice?wsdl");
//    print_r($client->__getFunctions());
//    print_r($client->__getTypes());

    $result = $client->__soapCall('pushWbData', array(array('in0' => "$result_data")));

    $time_end = microtime(true);
    echo 'webservice执行时间差为：' . ($time_end - $time_ago) . 's' . PHP_EOL;
    var_dump($result);
} catch (Exception $e) {
    print $e;
}

